﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using com.lover.astd.common.model;
using com.lover.astd.common.model.battle;

namespace com.lover.astd.game.ui.ui
{
    public partial class NpcSelector : Form
    {
        private List<Npc> _npcs;
        private List<string> _itemNames;
        private List<string> _itemColors;
        private List<Npc> _itemNpcs = new List<Npc>();
        private Npc _selectedNpc;
        private string _formation;
        public NpcSelector()
        {
            InitializeComponent();
        }
        public Npc getSelectedNpc()
        {
            return _selectedNpc;
        }
        public string getFormation()
        {
            return _formation;
        }
        public void setNpcs(List<Npc> npcs)
        {
            _npcs = npcs;
            _itemNames = new List<string>();
            _itemColors = new List<string>();
            foreach (Npc npc in npcs)
            {
                string itemname = npc.ItemNamePure;
                if (_itemNames.Contains(itemname)) continue;
                _itemNames.Add(itemname);
                _itemColors.Add(npc.ItemColor);
            }
            BindingSource src = new BindingSource();
            src.DataSource = _itemNames;
            lst_reason.DataSource = src;
            lst_reason.SelectedIndex = -1;
        }
        private void NpcSelector_Load(object sender, EventArgs e)
        {
        }
        private void renderNpcByItem(string itemname)
        {
            _itemNpcs.Clear();
            if (itemname != null && itemname.Length > 0)
            {
                foreach (Npc npc in _npcs)
                {
                    if (npc.ItemNamePure.Equals(itemname)) _itemNpcs.Add(npc);
                }
            }
            BindingSource src = new BindingSource();
            src.DataSource = _itemNpcs;
            lst_npc.DataSource = src;
            lst_npc.SelectedIndex = -1;
        }
        private void lst_reason_SelectedIndexChanged(object sender, EventArgs e)
        {
            string itemname = lst_reason.SelectedItem as string;
            renderNpcByItem(itemname);
        }

        private void lst_reason_DrawItem(object sender, DrawItemEventArgs e)
        {
            ListBox lst = (ListBox)sender;
            // Draw the background of the ListBox control for each item.
            e.DrawBackground();
            // Define the default color of the brush as black.
            Brush myBrush = Brushes.Black;

            // Determine the color of the brush to draw each item based 
            // on the index of the item to draw.
            int index = e.Index;
            if (index < 0) return;
            string itemcolor = _itemColors[index];
            string color = itemcolor.TrimStart('#');
            if (color != "")
            {
                Color _color = Color.FromArgb(
                   Int32.Parse(color.Substring(0, 2), NumberStyles.AllowHexSpecifier),
                   Int32.Parse(color.Substring(2, 2), NumberStyles.AllowHexSpecifier),
                   Int32.Parse(color.Substring(4, 2), NumberStyles.AllowHexSpecifier)
                   );
                myBrush = new SolidBrush(_color);

                // Draw the current item text based on the current Font 
                // and the custom brush settings.
                e.Graphics.DrawString(lst.Items[e.Index].ToString(),
                    e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
                // If the ListBox has focus, draw a focus rectangle around the selected item.
                e.DrawFocusRectangle();
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            int npcindex = lst_npc.SelectedIndex;
            if (npcindex >= 0) _selectedNpc = lst_npc.SelectedItem as Npc;
            if (combo_formation.SelectedIndex < 0) _formation = "不变阵";
            else _formation = combo_formation.SelectedItem as string;
            this.Close();
        }
    }
}
